#!/usr/bin/env python

import sys
import string
import glob

lines_before = 5

lines_after = lines_before

lines_of_context = lines_before + 1 + lines_after

print_next = 0


def grepfile(pattern, the_file):
    context = []
    lines = the_file.readlines()
    print_it = [0] * len(lines)
    
    for i in range(len(lines)):
        lines[i] = string.rstrip(lines[i])
        if -1 != string.find(lines[i], pattern):
            for j in range(i - lines_before, i + lines_after + 1):
                if 0 < j < len(lines):
                    print_it[j] = 1
                    
    results = []
    for i in range(len(lines)):
        if print_it[i]:
            results.append((i, lines[i]))
    return results
        

pattern = sys.argv[1]


if len(sys.argv) > 2:
    for arg in sys.argv[2:]:
        for filename in glob.glob(arg): 
            file = open(filename, 'r')
            results = grepfile(pattern, file)

            for (number, line) in results:
                print filename + '(' + str(number) + '): ' + line
else:
    results = grepfile(pattern, sys.stdin)
    for (number, line) in results:
        print  str(number) + ': ' + line
    
            
        
